//
//  MJ_MainViewController.m
//  NewClothesForEveryDay
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 蓝天. All rights reserved.
//

#import "MJ_MainViewController.h"
#import "AFNetworkHandle.h"
#import "BaseHeader.h"
#import "MJOnePicModel.h"
#import "MJOnePageModel.h"
#import "MJOneCollectionViewCell.h"
#import "MJ_DetailsViewController.h"


@interface MJ_MainViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) NSString *everyGroupStr;
@property (nonatomic, retain) NSMutableArray *myArr;
@property (nonatomic, assign) int numOfGroup;

@property (nonatomic, retain) UICollectionView *collection;

@end

@implementation MJ_MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.myArr = [NSMutableArray array];

    
    [self getDataArr];
}




- (void)getDataArr
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mrosevision.sinaapp.com/mfashion_api_v200.php?ac=getflow&id=20009&start=%d&djb=1&count=50", self.numOfGroup];
    [AFNetworkHandle GetwithAFNByUrl:urlStr completion:^(id result) {
        NSArray *tempArr = [result objectForKey:@"article"];
        self.myArr = [MJOnePageModel arrayWithModelByArray:tempArr];
        NSLog(@"%ld", self.myArr.count);
//        NSLog(@"%@", self.myArr);
//        [[self.myArr[0] simgs] objectAtIndex:0];
        [self makeCollection];
    }];
}

- (void)makeCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth - 10 - 3) / 2, ScreenWidth / 1.96);
    layout.minimumInteritemSpacing = 3;
    layout.minimumLineSpacing = 3;
    layout.sectionInset = UIEdgeInsetsMake(64, 5, 0, 5);
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
//    self.collection.backgroundColor = [UIColor cyanColor]; ////////
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    [self.collection registerClass:[MJOneCollectionViewCell class] forCellWithReuseIdentifier:@"LTA"];
                                          
    [layout release];
    [self.collection release];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MJOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LTA" forIndexPath:indexPath];

//    cell.labelTitle =[self.myArr[indexPath.item] title];
    NSString *str = [[[self.myArr[indexPath.item] simgs] objectAtIndex:0] objectForKey:@"url"];
    NSString *url = [NSString stringWithFormat:@"http://roseimg.b0.upaiyun.com/%@", str];
    
    
    cell.picUrl = url;
     [cell.img setContentMode:UIViewContentModeBottom];
    [cell setLabelAndImg];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
        NSLog(@"%ld, %ld", indexPath.section, indexPath.item);
    MJ_DetailsViewController *VC = [[[MJ_DetailsViewController alloc] init] autorelease];
    VC.myId = [self.myArr[indexPath.item] myId];
    
    [self.navigationController pushViewController:VC animated:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
